# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>
# Contributor: Tim S. <stahta01@gmail.com>

# Note: Added "ENABLE_itk=OFF" because of random build errors;
# It appeared to think it found the itk library; but, it then errored out.
# Set "ENABLE_qt=OFF" and "DEFAULT_NO_CAIRO_DEVICES=ON" to reduce warnings.
# Had to define PLD_png and etc to ON to get libGD to be searched.

_realname=plplot
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.15.0
pkgrel=5
arch=('any')
pkgdesc="Scientific plotting software (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-numpy"
         "${MINGW_PACKAGE_PREFIX}-shapelib"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets"
         "${MINGW_PACKAGE_PREFIX}-qhull"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-libgd"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             #"${MINGW_PACKAGE_PREFIX}-gcc-ada"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "make"
             "patch"
             "perl"
             "bsdtar>=3.3"
             "${MINGW_PACKAGE_PREFIX}-cmake>=3.6.2")
optdepends=("${MINGW_PACKAGE_PREFIX}-swig: connects Plplot C library to Python and Lua"
            "${MINGW_PACKAGE_PREFIX}-libgd: ability to output png, jpeg and gif files")
options=('strip' 'staticlibs')
license=('LGPL')
url="https://plplot.sourceforge.io/"
source=(https://downloads.sourceforge.net/sourceforge/plplot/${_realname}-${pkgver}.tar.gz
        find-gd.patch
        Include-wx-msw-wrapwin.h-instead-of-Windows.h.patch
        pdf-linking-libharu.patch
        fix-pkgconfig.patch
        fix-install-dir.patch
        fix-cmake-windows-libs.patch
        fix-wingdi.patch)
sha256sums=('b92de4d8f626a9b20c84fc94f4f6a9976edd76e33fb1eae44f6804bdcc628c7b'
            '57ad3a43d5ad467b89889a42d0b033e35cf322b9b83f2f41a62fe5d74a5d01b8'
            'fc27681c6ca521308ac0d873b3ed78c335e676ca6d367fd8d000a41580565ed8'
            'abf5ee33dad2108220d0973de4279ce04007b9fb429184d194a6cf951005c8be'
            'fe7a7c0e888187a00c2df8e42c54ed964845de2aa5e8fc2fdf94868c0cfb148c'
            'c11a37adea8134f9497fd45f24cd153cce18855ddff66ea5ba6ef8875da022fc'
            '4bef0104cc89bc09dbb7e05e06ac86a37621402b512fd4fa7ce81d0c6c8fab72'
            '675b7bdf421a81ba390b023dc94a0d43604180fa60a88364c6883fb02a950acc')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/find-gd.patch
  patch -p1 -i ${srcdir}/Include-wx-msw-wrapwin.h-instead-of-Windows.h.patch
  patch -p1 -i ${srcdir}/pdf-linking-libharu.patch
  patch -p1 -i ${srcdir}/fix-pkgconfig.patch
  #patch -p1 -i ${srcdir}/fix-qhull-lib.patch
  patch -p1 -i ${srcdir}/fix-install-dir.patch
  patch -p1 -i ${srcdir}/fix-cmake-windows-libs.patch
  patch -p1 -i ${srcdir}/fix-wingdi.patch
}

build() {
  # cmake may be confused if there is a Windows installation
  # of the official Python MSI. Please do the Python-enabled
  # build only, if no official Python is installed.
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  local pyexe="${MINGW_PREFIX}/bin/python3.exe"
  local pyver=$(${pyexe} -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  local pylib="${MINGW_PREFIX}/lib/libpython${pyver}.dll.a"
  if [[ ${pyver:0:1} != "3" ]] ; then
     pylib="${MINGW_PREFIX}/lib/libpython${pyver}m.dll.a"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_Fortran_COMPILER=${MINGW_PREFIX}/bin/gfortran.exe \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TEST=OFF \
    -DPYTHON_VERSION=${pyver} \
    -DPYTHON_EXECUTABLE=${pyexe} \
    -DPYTHON_LIBRARY=${pylib} \
    -DPYTHON_INCLUDE_DIRS=${MINGW_PREFIX}/include/python${pyver} \
    -DQHULL_INCLUDE_DIRS=${MINGW_PREFIX}/include \
    -DQHULL_LIBRARY_DIRS=${MINGW_PREFIX}/lib \
    -DQHULL_LIBRARIES=qhullstatic \
    -DGD_INCLUDE_DIR=${MINGW_PREFIX}/include \
    -DDEFAULT_NO_BINDINGS=ON \
    -DENABLE_ada=OFF \
    -DENABLE_cxx=ON \
    -DENABLE_fortran=ON \
    -DENABLE_lua=ON \
    -DENABLE_python=ON \
    -DENABLE_tcl=ON \
    -DENABLE_tk=ON \
    -DENABLE_DYNDRIVERS=OFF \
    -DENABLE_wxwidgets=ON \
    -DENABLE_itk=OFF \
    -DENABLE_qt=OFF \
    -DPLD_png=ON \
    -DPLD_jpeg=ON \
    -DPLD_gif==ON \
    -DPLD_wingcc=ON \
    -DPLD_wingdi=ON \
    ../${_realname}-${pkgver}

  make V=1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  local MINGW_PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  local MSYS2_USR_WIN=$(cygpath -am /usr)

  # Fix paths in pkgconfig files
  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  # Fix paths in cmake files
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/plplot/*.cmake; do
    sed -e "s|${MINGW_PREFIX_WIN}|\$\{MINGW_PREFIX\}|g" -i ${_f}
  done

  # Fix paths in example files
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name '*.sh' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name 'Makefile' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name '*.sh' \
    -exec sed -i -e "s|${MSYS2_USR_WIN}|/usr|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name 'Makefile' \
    -exec sed -i -e "s|${MSYS2_USR_WIN}|/usr|g" {} \;

  # Fix paths in the remaing odd files
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tcl/tclsh_standard_examples
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_runAllDemos
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_standard_examples
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/cmake/modules/plplot_configure.cmake
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/cmake/modules/plplot_configure.cmake
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tcl/pltcl_standard_examples
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tcl/tclsh_standard_examples
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/plserver_runAllDemos
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/plserver_standard_examples
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_runAllDemos
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_standard_examples

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname
  cp -pf ${srcdir}/${_realname}-${pkgver}/Copyright \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
  cp -pf ${srcdir}/${_realname}-${pkgver}/COPYING* \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
}
